# 65. 悄悄话

65 65-1 65-2

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on('line', function(line) {
    const arr = line.split(' ').map(Number);
    let maxTime = 0;
    const queue = [];
    queue.push(0);
    while(queue.length) {
        const parentIndex = queue.shift();
        const leftChildIndex = 2*parentIndex + 1;
        const rightChildIndex = 2*parentIndex + 2;
        if (arr[leftChildIndex] && arr[leftChildIndex] > -1) {
            arr[leftChildIndex] += arr[parentIndex];
            queue.push(leftChildIndex);
            maxTime = Math.max(maxTime, arr[leftChildIndex]);
        }
        if (arr[rightChildIndex] && arr[rightChildIndex] > -1) {
            arr[rightChildIndex] += arr[parentIndex];
            queue.push(rightChildIndex);
            maxTime = Math.max(maxTime, arr[rightChildIndex]);
        }
    }
    console.log(maxTime);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28